//
//  TaskListViewController.swift
//  XXIOT
//
//  Created by xx on 2024/8/20.
//

import UIKit
import FastSwiftSDK

class TaskListViewController: FastViewController,UITableViewDelegate,UITableViewDataSource {
    
    var mac = "" // mac地址
    
    private lazy var view_add : AddTaskView = {
        let view =  AddTaskView()
        view.backgroundColor = .clear
        view.isHidden = true
        view.addBlcok = {[weak self] dic in
            FastLog.debug(dic)
            view.isHidden = true
        }
        view.cancelBlcok = {
            view.isHidden = true
        }
        self.view.addSubview(view)
        return  view
    } ()
    
    private lazy var btn_add : FastButton = {
        let btn =  FastButton.init(title: "添加任务", imgName: "",color: .white ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.backgroundColor = themeColor
        btn.click = { [weak self] in
            self?.add()
        }
        self.view.addSubview(btn)
        return  btn
    } ()
    
    
    private lazy var tableView : UITableView = {
        let view = UITableView.init(frame: .zero, style: .plain)
        view.delegate = self
        view.dataSource = self
        self.view.addSubview(view)
        return  view
    } ()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "任务"
        
    }
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        tableView.snp.makeConstraints { make in
            make.top.equalTo(kNavH)
            make.bottom.equalTo(-60)
            make.left.right.equalToSuperview()
        }
        view_add.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        btn_add.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.bottom.equalTo(-20-kSafeH)
            make.height.equalTo(btnHeigth)
        }
    }
    
    //MARK: action
    func add(){
        view_add.isHidden = false
    }
    
    @objc func handleLongPress(sender:UILongPressGestureRecognizer){
        if sender.state == .ended{
            FastAlert.shared.showAlert(on: self, title: "删除任务", message:"开   8:00   每天" , actions: [.cancelStyle("否"),.defStyle("是")])
            FastAlert.shared.callBack = { num in
                print(num)
            }
        }
    }
    //MARK: netWork
    func getTaskList(){// 获取列表
        
    }
    func deleteTask(){// 删除
        
    }
    func addTask(){// 添加
        
    }
    //MARK: delegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell.init()
        cell.textLabel?.text = "开   8:00   每天"
        cell.tag = indexPath.row
        let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
        //        longPressGesture.delegate = self
        cell.addGestureRecognizer(longPressGesture)
        return cell
    }
    
}

class AddTaskView:FastView {
    var typeIdnex = 0 // 0 每天   1工作日   2单次
    var addBlcok:FastDicBlock?
    var cancelBlcok:FastVoidBlock?
    
    private lazy var view_bg : UIView = {
        let view =  UIView()
        view.backgroundColor = .hex("000000", 0.3)
        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(cancel)))
        self.addSubview(view)
        return  view
    } ()
    
    private lazy var view_main : UIView = {
        let view =  UIView()
        view.backgroundColor = .white
        view.cornerRadius = 5
        self.addSubview(view)
        return  view
    } ()
    
    private lazy var btn_switch : FastButton = {
        let btn =  FastButton.init(title: "开", imgName: "",color: .white ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.backgroundColor = themeColor
        btn.tag = 1
        btn.click = { [weak self] in
            self?.switchAction()
        }
        self.view_main.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_add : FastButton = {
        let btn =  FastButton.init(title: "添加", imgName: "",color: .white ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.backgroundColor = themeColor
        btn.click = { [weak self] in
            self?.add()
        }
        self.view_main.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_day : FastButton = {
        let btn =  FastButton.init(title: "每天", imgName: "",color: .x333 ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.backgroundColor = .xF8
        btn.click = { [weak self] in
            self?.typeAction(index: 0)
        }
        self.view_main.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_work : FastButton = {
        let btn =  FastButton.init(title: "工作日", imgName: "",color: .x333 ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.backgroundColor = .xF8
        btn.click = { [weak self] in
            self?.typeAction(index: 1)
        }
        self.view_main.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_onec : FastButton = {
        let btn =  FastButton.init(title: "单次", imgName: "",color: .x333 ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.backgroundColor = .xF8
        btn.click = { [weak self] in
            self?.typeAction(index: 2)
        }
        self.view_main.addSubview(btn)
        return  btn
    } ()
    
    private lazy var txt_time : FastTextField = {
        let txt = FastTextField.init(color: .x333,placeholder: "请选择时间")
        txt.inputView = self.datePicker
        self.view_main.addSubview(txt)
        return  txt
    } ()
    
    private lazy var datePicker : UIDatePicker = {
        let datePicker = UIDatePicker()
        datePicker.datePickerMode = .time
        datePicker.addTarget(self, action: #selector(dateChanged), for: .valueChanged)
        return  datePicker
    } ()

    override func layoutSubviews() {
        view_bg.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        view_main.snp.makeConstraints { make in
            make.size.equalTo(300)
            make.center.equalToSuperview()
        }
        
        btn_switch.snp.makeConstraints { make in
            make.top.left.equalTo(20)
            make.width.equalTo(45)
            make.height.equalTo(35)
        }
        
        txt_time.snp.makeConstraints { make in
            make.top.equalTo(btn_switch.snp.bottom).offset(20)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(40)
        }
        
        btn_day.snp.makeConstraints { make in
            make.top.equalTo(txt_time.snp.bottom).offset(20)
            make.left.equalTo(20)
            make.width.equalTo(60)
            make.height.equalTo(30)
        }
        
        btn_work.snp.makeConstraints { make in
            make.size.top.equalTo(btn_day)
            make.left.equalTo(btn_day.snp.right).offset(5)
        }
        
        btn_onec.snp.makeConstraints { make in
            make.size.top.equalTo(btn_day)
            make.left.equalTo(btn_work.snp.right).offset(5)
        }
        
        btn_add.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(btnHeigth)
            make.bottom.equalTo(-20)
        }

    }
    
    //MARK: Action
    @objc func dateChanged(){
        // 格式化时间并更新文本框
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "HH:mm" // 24小时制
        txt_time.text = dateFormatter.string(from: datePicker.date)
    }
    
    func switchAction(){
        if btn_switch.tag == 0{
            btn_switch.tag = 1
        }else{
            btn_switch.tag = 0
        }
        if btn_switch.tag == 0{
            btn_switch.backgroundColor = .xF8
            btn_switch.title = "关"
            btn_switch.titleColor = .x333
        }else{
            btn_switch.backgroundColor = themeColor
            btn_switch.title = "开"
            btn_switch.titleColor = .white
        }
    }
    
    func typeAction(index:Int){
        typeIdnex = index
        btn_day.backgroundColor = .xF8
        btn_day.titleColor = .x333
        btn_work.backgroundColor = .xF8
        btn_work.titleColor = .x333
        btn_onec.backgroundColor = .xF8
        btn_onec.titleColor = .x333
        if typeIdnex == 0{
            btn_day.backgroundColor = themeColor
            btn_day.titleColor = .white
        }
        if typeIdnex == 1{
            btn_work.backgroundColor = themeColor
            btn_work.titleColor = .white
        }
        if typeIdnex == 2{
            btn_onec.backgroundColor = themeColor
            btn_onec.titleColor = .white
        }
    }
    
    func add(){
        if isEmpty(str: txt_time.text) == true{
            FastHUD.showErr("请选择时间")
            return
        }
        
        var loopType = ""
        if typeIdnex == 0{
            loopType = "day"
        }else if typeIdnex == 1{
            loopType = "weekday"
        }else if typeIdnex == 2{
            loopType = "once"
        }
        var msg = ""
        if btn_switch.tag == 0{
            msg = "OFF"
        }else{
            msg = "ON"
        }
        
        let dic = ["loop":loopType,"msg":msg,"time":txt_time.text!]
        self.addBlcok?(dic)
    }
    
    @objc func cancel(sender:UITapGestureRecognizer){
        self.cancelBlcok?()
    }
    
}
    
